package com.pri.chat.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.DTBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.vedio.Utility;
import com.pri.chat.model.ZanModel;
import com.pri.chat.utils.GlideUtil;
import com.pri.chat.view.ImageViewPlus;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.vondear.rxtool.view.RxToast;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/7
 */
public class VideoListAdapter1 extends BaseQuickAdapter<DTBean, BaseViewHolder> {
    private int actual_width;

    public VideoListAdapter1(int layoutResId, @Nullable List<DTBean> data) {
        super(layoutResId, data);
    }

    /**
     * 加载视频帧图
     *
     * @param imageView 展示图片view
     * @param url       视频链接
     * @param context   上下文
     */
    public static void loadCover(ImageView imageView, String url, Context context) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.img_default)//图片加载出来前，显示的图片
                .fallback(R.mipmap.img_default) //url为空的时候,显示的图片
                .error(R.mipmap.img_default);
//                .transform(new CenterCrop(), new RoundedCorners(10));//图片加载失败后，显示的图片
        Glide.with(context).load(url).apply(options).into(imageView);

    }

    @Override
    protected void convert(BaseViewHolder holder, DTBean item) {
        ImageView item_img = holder.getView(R.id.item_img);
        ImageView item_bg = holder.getView(R.id.item_bg);
//        loadCover(holder.getView(R.id.item_img), item.getVideoPath() + "?vframe/jpg/offset/1", mContext);
//        loadCover(holder.getView(R.id.item_bg), item.getVideoPath() + "?vframe/jpg/offset/1", mContext);

        int adapterPosition = holder.getAdapterPosition();
        int itemHeight;
        if (0 == adapterPosition) {
            itemHeight = Utility.dp2px(holder.itemView.getContext(), 200);
            //高度一致则无需再次设置
            if (itemHeight != holder.itemView.getLayoutParams().height)
                holder.itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight));
        } else {
            itemHeight = Utility.dp2px(holder.itemView.getContext(), 245);
            //高度一致则无需再次设置
            if (itemHeight != holder.itemView.getLayoutParams().height)
                holder.itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight));
        }

        if (actual_width <= 0)
            actual_width = (Utility.getsW((Activity) mContext) - Utility.dp2px(holder.itemView.getContext(), 8) / 2);



        holder.setText(R.id.item_title,item.getContent());
        holder.setText(R.id.tv_video_zan, item.getZanNum() + "");
        holder.setText(R.id.pl_num, item.getPlNum() + "");
        ImageView zanImage = holder.getView(R.id.zanImage);
        if (item.getSfdz() == 1) {
            zanImage.setImageResource(R.mipmap.ic_zan_selected);
        } else {
            zanImage.setImageResource(R.mipmap.ic_zan_normal);
        }

        Glide.with(holder.itemView.getContext())
                .asDrawable()
                .load(item.getVideoPath() + "?vframe/jpg/offset/1")
                .placeholder(R.drawable.shape_black)
                .error(R.drawable.shape_black)
                .into(item_bg);

        Glide.with(holder.itemView.getContext())
                .asDrawable()
                .load(item.getVideoPath() + "?vframe/jpg/offset/1")
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                        if (null == item_img || null == holder || null == holder.itemView)
                            return false;
                        int image_width = resource.getIntrinsicWidth();
                        int image_height = resource.getIntrinsicHeight();

                        int scale = actual_width / image_width;
                        int iamgHeight = image_height * (scale > 1 ? 1 : scale);
                        if (iamgHeight > 0) {
                            ViewGroup.LayoutParams params = item_img.getLayoutParams();
                            params.height = iamgHeight;
                            item_img.setLayoutParams(params);
                        }
                        return false;
                    }
                })
                .into(item_img);
    }
}
